// Copyright (C) joyinter
// All rights reserved.
// 
// Author : Jiang Li
// Date   : 2004-6-18

#ifndef __UTILITY_H__
#define __UTILITY_H__

#pragma once

CString JoyGetModuleDir(HMODULE hMod);
CString JoyGetAppDir();
CString JoyGetFileName(const CString& path);
CString JoyGetFileDir(const CString& path);


template<>
AFX_INLINE UINT AFXAPI HashKey(const CString& str)
{
	// default identity hash - works for most primitive values
	return HashKey<PCSTR>(str);
}

template<class KEY, class ARG_KEY, class VALUE, class ARG_VALUE>
void JoyClearPtrMap(CMap<KEY, ARG_KEY, VALUE, ARG_VALUE>& m)
{
    POSITION pos = m.GetStartPosition();
    while(pos != NULL)
    {
        KEY k;
        VALUE v;
        m.GetNextAssoc(pos, k, v);
        if(v != NULL)
        {
            delete v;
        }
    }
    m.RemoveAll();
}

template<class KEY, class ARG_KEY, class VALUE, class ARG_VALUE>
void JoyReleasePtrMap(CMap<KEY, ARG_KEY, VALUE, ARG_VALUE>& m)
{
    POSITION pos = m.GetStartPosition();
    while(pos != NULL)
    {
        KEY k;
        VALUE v;
        m.GetNextAssoc(pos, k, v);
        if(v != NULL)
        {
            v->Release();
        }
    }
    m.RemoveAll();
}

template<class TYPE, class ARG_TYPE>
void JoyClearPtrList(CList<TYPE*, ARG_TYPE> & l)
{
    POSITION pos = l.GetHeadPosition();
    while(pos != NULL)
    {
        TYPE* p = l.GetNext(pos);
        if(p != NULL)
        {
            delete p;
        }
    }
    l.RemoveAll();
}

void JoyProcessMessage();

template<typename T>
void JoyConstruct(T* p)
{
#pragma push_macro("new")
#ifdef new
#undef new
#endif

    new(p) T;

#pragma pop_macro("new")
}

void JoyConvertStr(CStringW& dst, const CString& src);
void JoyConvertStr(CString& dst, const CStringW& src);
void JoySplitStr(CArray<CString>& result, const CString& s, char c);

HANDLE JoyFindProcess(const CString& fileName);

#endif  // __UTILITY_H__